<template>
	<view class="body-view">
		<!--导航和状态栏-->
		<view class="state-navigation">
			<view class="slim_header_navigation d-flex a-center j-center" >
				<view class="left_box" @tap="goBack">
					<image src="/static/center/back.png" class="back"></image>
				</view>
				<view class="title">隐私设置</view>
				<view class="right_box">
					<!-- <image src="/static/center/dian.png" class="dian"></image> -->
				</view>
			</view>
		</view>
		<!-- 顶部位置占位 -->
		<view style="height:calc(100rpx + var(--status-bar-height));"></view>
		<view class="top-row d-flex a-center j-between">
			<view class="row-title">加我好友需要审核</view>
			<tm-switch
                        linear="left"
                        linearDeep="accent"
                        v-model="needAuth"
                        :defaultValue="needAuth"
                        @change="handleChange"
                        color="rgba(15, 95, 70, 0.30)"
                        :barColor="
                            needAuth
                                ? 'rgba(35, 226, 166, 1)'
                                : 'rgba(195, 204, 202, 1)'
                        "
                        barIcon=""
                        size="normal"
                    ></tm-switch>
		</view>
		<view style="height:20rpx;"></view>
		<view class="top-row d-flex a-center">
			<view class="row-title">添加我的方式</view>
		</view>
		<view class="top-row d-flex a-center j-between">
			<view class="row-title distance">ID搜索</view>
			<tm-switch
                        linear="left"
                        linearDeep="accent"
                        v-model="isId"
                        :defaultValue="isId"
                        @change="handleChangeIsId"
                        color="rgba(15, 95, 70, 0.30)"
                        :barColor="
                            isId
                                ? 'rgba(35, 226, 166, 1)'
                                : 'rgba(195, 204, 202, 1)'
                        "
                        barIcon=""
                        size="normal"
                    ></tm-switch>
		</view>
		<view class="top-row d-flex a-center j-between">
			<view class="row-title distance">手机号</view>
			<tm-switch
                        linear="left"
                        linearDeep="accent"
                        v-model="isPhone"
                        :defaultValue="isPhone"
                        @change="handleChangeIsPhone"
                        color="rgba(15, 95, 70, 0.30)"
                        :barColor="
                            isPhone
                                ? 'rgba(35, 226, 166, 1)'
                                : 'rgba(195, 204, 202, 1)'
                        "
                        barIcon=""
                        size="normal"
                    ></tm-switch>
		</view>
		<view class="top-row d-flex a-center j-between">
			<view class="row-title distance">二维码</view>
			<tm-switch
                        linear="left"
                        linearDeep="accent"
                        v-model="isQrcode"
                        :defaultValue="isQrcode"
                        @change="handleChangeIsQrcode"
                        color="rgba(15, 95, 70, 0.30)"
                        :barColor="
                            isQrcode
                                ? 'rgba(35, 226, 166, 1)'
                                : 'rgba(195, 204, 202, 1)'
                        "
                        barIcon=""
                        size="normal"
                    ></tm-switch>
		</view>
		<view class="top-row d-flex a-center j-between">
			<view class="row-title distance">群</view>
            <tm-switch
                        linear="left"
                        linearDeep="accent"
                        v-model="isGroup"
                        :defaultValue="isGroup"
                        @change="handleChangeIsGroup"
                        color="rgba(15, 95, 70, 0.30)"
                        :barColor="
                            isGroup
                                ? 'rgba(35, 226, 166, 1)'
                                : 'rgba(195, 204, 202, 1)'
                        "
                        barIcon=""
                        size="normal"
                    ></tm-switch>
		</view>
		<view class="top-row d-flex a-center j-between">
			<view class="row-title distance">出现在附近人列表</view>
		    <tm-switch
		                linear="left"
		                linearDeep="accent"
		                v-model="isNear"
		                :defaultValue="isNear"
		                @change="handleChangeIsNear"
		                color="rgba(15, 95, 70, 0.30)"
		                :barColor="
		                    isNear
		                        ? 'rgba(35, 226, 166, 1)'
		                        : 'rgba(195, 204, 202, 1)'
		                "
		                barIcon=""
		                size="normal"
		            ></tm-switch>
		</view>
	</view>
</template>

<script setup lang="tsx" name="privacySetting">
import {
    getMyProfile,
    updateMyProfile,
} from "@/hooks/imHooks";
import { ref } from "vue";
import { userData, } from "@/store/userData";
import {
    onShow,
    onLoad,
} from "@dcloudio/uni-app";
import { getUserSecret, updateUserSecret} from "@/api/api"

const needAuth = ref(false);

const USER_DATA = userData();


const handleChange = async (bool:boolean) =>{
    needAuth.value = bool;
    const params = {
        allowType: needAuth.value ? uni.$TIM.TYPES.ALLOW_TYPE_NEED_CONFIRM:uni.$TIM.TYPES.ALLOW_TYPE_ALLOW_ANY
    }
    updateMyProfile(params);
}

const getNeedAuth = async () => {
    const {code,data} = await getMyProfile();   
    if(code!==0){
        uni.showToast({
            icon: "none",
            title: "获取验证方式失败",
            duration: 1500,
        });
        return;
    }

    const { allowType } = data;

    if(allowType==="AllowType_Type_AllowAny"){
        needAuth.value = false;
    }else{
        needAuth.value = true;
    }
};

const isId = ref(false)
const isPhone = ref(false)
const isQrcode = ref(false)
const isGroup = ref(false)
const isNear = ref(false)

const GetUserSecret = async () => {
    const params = { userId: USER_DATA.id}
    const  res:any = await getUserSecret(params)
    const {flag,data} = res;
    if(flag!==1){
        uni.showToast({
            icon: "none",
            title: "获取添加方式失败",
            duration: 1500,
        });
        return;
    }


    isId.value = !!data.isId
    isPhone.value = !!data.isPhone
    isQrcode.value = !!data.isQrcode
    isGroup.value = !!data.isGroup
	isNear.value = !!data.isNear
}


const handleChangeIsId = async (bool:boolean) =>{
    isId.value = bool;
    UpdateUserSecret();
}
const handleChangeIsPhone = async (bool:boolean) =>{
    isPhone.value = bool;
    UpdateUserSecret();
}
const handleChangeIsQrcode = async (bool:boolean) =>{
    isQrcode.value = bool;
    UpdateUserSecret();
}
const handleChangeIsGroup = async (bool:boolean) =>{
    isGroup.value = bool;
    UpdateUserSecret();
}
const handleChangeIsNear = async (bool:boolean) =>{
    isNear.value = bool;
    UpdateUserSecret();
}

const UpdateUserSecret = async () => {
    const param = {
        userId:USER_DATA.id,
        isId:isId.value?1:0,
        isPhone:isPhone.value?1:0,
        isQrcode:isQrcode.value?1:0,
        isGroup:isGroup.value?1:0,
		isNear:isNear.value?1:0,
    }
    await updateUserSecret(param)
}
onLoad(() => {
    getNeedAuth();
    GetUserSecret();
});





const goBack = () => {
	uni.navigateBack();
};
</script>

<style lang="scss" scoped>
@import './privacySetting.scss';
</style>
